package com.nzf.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzf.vote.entity.UserVote;
import com.nzf.vote.entity.Vote;
import com.nzf.vote.mapper.UserVoteMapper;
import com.nzf.vote.mapper.VoteMapper;
import com.nzf.vote.model.dto.OptionDto;
import com.nzf.vote.model.dto.RankingDto;
import com.nzf.vote.model.dto.VoteDto;
import com.nzf.vote.service.ITypeService;
import com.nzf.vote.service.IUserService;
import com.nzf.vote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {
    @Autowired
    VoteMapper voteMapper;
    @Autowired
    UserVoteMapper userVoteMapper;
    @Autowired
    ITypeService typeService;
    @Autowired
    IUserService userService;

    @Override
    public List<VoteDto> VoteList() {
        List<VoteDto> voteDtos =new ArrayList<>();
        voteMapper.selectList(null).forEach(vote -> {
            VoteDto voteDto = new VoteDto();
            voteDto.setId(vote.getId());
            voteDto.setTheme(vote.getTheme());
            voteDto.setNum(vote.getNum());
            voteDto.setThemeInfo(vote.getThemeInfo());
            voteDto.setThemeType(typeService.getById(vote.getThemeTypeId()).getType());
            voteDto.setUsername(userService.getById(vote.getUserId()).getName());
            String[] split = vote.getMyOption().split(",");
            List<OptionDto> optionDto = new ArrayList<>();
            List<UserVote> userVoteByVoteId = userVoteMapper.getUserVoteByVoteId(vote.getId());
            voteDto.setCreateTime(vote.getCreateTime());
            for (int i = 0; i < split.length; i++) {
                int finalI = i;
                int num = (int) userVoteByVoteId.stream().filter(userVote -> userVote.getMyOption().equals(split[finalI])).count();
                OptionDto p = new OptionDto();
                p.setOption(split[i]);
                p.setNum(num);
                optionDto.add(p);
            }
            voteDto.setOption(optionDto);
            voteDtos.add(voteDto);
        });
        return voteDtos;
    }

    @Override
    public List<Vote>getVoteByUserId(Integer userId) {
        return voteMapper.getVoteByUserId(userId);
    }

    @Override
    public VoteDto getVoteById(Integer id) {
        VoteDto voteDto = new VoteDto();
        Vote vote = voteMapper.selectById(id);
        voteDto.setId(vote.getId());
        voteDto.setTheme(vote.getTheme());
        voteDto.setNum(vote.getNum());
        voteDto.setThemeInfo(vote.getThemeInfo());
        voteDto.setThemeType(typeService.getById(vote.getThemeTypeId()).getType());
        voteDto.setUsername(userService.getById(vote.getUserId()).getName());
        String[] split = vote.getMyOption().split(",");
        List<OptionDto> optionDto = new ArrayList<>();
        List<UserVote> userVoteByVoteId = userVoteMapper.getUserVoteByVoteId(vote.getId());
        voteDto.setCreateTime(vote.getCreateTime());
        for (int i = 0; i < split.length; i++) {
            int finalI = i;
            int num = (int) userVoteByVoteId.stream().filter(userVote -> userVote.getMyOption().equals(split[finalI])).count();
            OptionDto p = new OptionDto();
            p.setOption(split[i]);
            p.setNum(num);
            optionDto.add(p);
        }
        voteDto.setOption(optionDto);
        return voteDto;
    }

    @Override
    public int deleteVote(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("vote_id",id);
        List<UserVote> list = userVoteMapper.selectList(queryWrapper);
        list.forEach(userVote -> {
            userVoteMapper.deleteById(userVote.getId());
        });
        int i = voteMapper.deleteById(id);
        return i;
    }

    @Override
    public RankingDto getVoteRanking() {
        RankingDto rankingDto = new RankingDto();
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        voteMapper.selectList(null).forEach(vote -> {
            nameList.add(vote.getTheme());
            valueList.add(vote.getNum());
        });
        rankingDto.setNameList(nameList);
        rankingDto.setValue(valueList);
        return rankingDto;
    }
}
